<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Admin\User;
use Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use DB;


use Illuminate\Support\Facades\Input;
class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //获取关键字数据
        $input =$request ->input('keywords')?$request->input('keywords'):'';

        $user =User::where('username','like','%'.$input.'%')->paginate(5);
       //分页获取数据
        return view('admin.users.index',compact('user','input'),['caption'=>'后台用户管理','block'=>'用户添加']);
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //引用模板
        return view('admin.users.create',['caption'=>'后台用户管理','block'=>'用户添加']);
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        //获取前台表单提交数据
       $input = $request -> except('_token');
       // dd($input);

      //表单验证
       $rule = [
            'username' => 'required|unique:admin_user|regex:/^[a-zA-Z0-9_]{6,18}$/',
            'password' => 'required|regex:/^[a-zA-Z0-9_]{6,18}$/',
        ];

        //表单验证错误信息提示
      
        $msg = [
            'username.required'=>'必须输入用户名',
            'username.regex'=>'用户名填写格式不规范',
            'username.unique'=>'用户名冲突',
            'password.required'=>'必须输入密码',
            'password.regex'=>'密码格式不规范'
        ];

        // validator::make() 处理错误消息
       $validator = Validator::make($input,$rule,$msg);

       //如果请求没有通过验证，则可以使用 withErrors 方法把错误消息闪存到 session。在进行重定向之后，$errors 变量可以在视图中自动共用，让你可以轻松地显示这些消息并返回给用户
       
        if ($validator->fails()) {
            return redirect('admin/users/create')
                        ->withErrors($validator)
                        ->withInput();
        }


        //将数据插入数据库
        //密码加密
        $pass = Crypt::encrypt($input['password']);

        //记住这个地方返回的是刚添加的那条记录
        $input = ['username'=>$input['username'],'password'=>$pass];
        $re =  User::create($input);
        //判断
          if($re){

              return redirect('admin/users');
          }else{
              return back()->with('msg','添加失败');
          }       

    }

    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //1 根据ID找到要修改的数据
        $user =User::find($id);


        $caption = '后台用户管理';
        $block = '用户列表';
        return view('admin.users.edit',compact('user','caption','block'));
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //获取数据
        $username =$request ->input('username');
        $user =User::find($id);
        $re =$user->update(['username'=>$username]);
        //判断
        if($re){
            return redirect('admin/users');
        }else{
            return back()->with('msg','修改错误');
        }
        //
    }

    /**
     * 删除
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $data = [];
        $re = User::find($id)->delete();
        // dd($re);
        if($re){
            $data=[
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }

        return $data;
        //
    }
}
